from django.db import models
from imagekit.models import ProcessedImageField
from imagekit.processors import ResizeToFill


class Course(models.Model):
    title = models.CharField(max_length=128,verbose_name="标题")
    created_time = models.DateTimeField(auto_now_add=True)
    is_finished = models.BooleanField(default=False,verbose_name="已完结")
    avatar_thumbnail = ProcessedImageField(
        upload_to = 'course/%Y%m%d',
        processors = [ResizeToFill(320,200)],
        format = 'JPEG',
        options = {'quality': 100},
        blank = True,
        null = True,
        verbose_name = "缩略图",
    )
    url = models.URLField(blank=True,verbose_name="链接")
    views = models.PositiveIntegerField(default=0,verbose_name='浏览量', editable=False)

    class Meta:
        ordering = ['created_time']
        verbose_name = "教程"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


    def increase_views(self):
        self.views += 1
        self.save(update_fields=['views'])


